package com.bw.liuyushan.zhuangao4_weektest2.UI;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.liuyushan.zhuangao4_weektest2.DaoSQL.Video;
import com.bw.liuyushan.zhuangao4_weektest2.DaoSQL.VideoMsgUtils;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Contrack.IContrackTest;
import com.bw.liuyushan.zhuangao4_weektest2.R;
import com.kd.easybarrage.Barrage;
import com.kd.easybarrage.BarrageView;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

/**
 * ZhuanGao4_WeekTest2
 * file name is : MyStand
 * created by Ender on 2021/8/23 10:39.
 * author : 刘雨杉
 * Describe(作用):
 */
public class MyStand extends StandardGSYVideoPlayer{
    long time=0;

    public MyStand(Context context, Boolean fullFlag) {
        super(context, fullFlag);
    }

    public MyStand(Context context) {
        super(context);
    }

    public MyStand(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()){//当前意图是
            case MotionEvent.ACTION_DOWN://点击时
                if (time==0){
                    time=System.currentTimeMillis();//点击时候获取当前时间点

                    //点击空心
                    findViewById(R.id.xinkk).setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            findViewById(R.id.xin2).setVisibility(VISIBLE);
                            findViewById(R.id.xinkk).setVisibility(INVISIBLE);
                        }
                    });
                    //点击红心
                    findViewById(R.id.xin2).setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            findViewById(R.id.xin2).setVisibility(INVISIBLE);
                            findViewById(R.id.xinkk).setVisibility(VISIBLE);
                        }
                    });


                }else{//第一次点击时的当前时间-第二次的当前时间比200毫秒少则判断是连续点击即双击
                    if (System.currentTimeMillis()-time < 200){
                        //生成图片 获取鼠标点击的xy轴 就是鼠标点击哪里图案在哪生成
                        ImageView imageView = new ImageView(getContext());
                        imageView.setX(event.getX());//!!!!!event.getX()
                        imageView.setY(event.getY());
                        imageView.setImageDrawable(getContext().getDrawable(R.drawable.xin2));//设置生成图片
                        imageView.setLayoutParams(new ViewGroup.LayoutParams(60,60));//设置生成图片大小
                        addView(imageView);//添加到视图
                        ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 1, 4);
                        ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 1, 4);
                        scaleX.setDuration(400);
                        scaleY.setDuration(400);
                        scaleX.start();
                        scaleY.start();
                        scaleX.addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {//动画完成时的监听事件
                                super.onAnimationEnd(animation);
                                //透明消失效果
                                ObjectAnimator.ofFloat(imageView,"alpha",1,0).setDuration(200).start();
                            }
                        });
                        findViewById(R.id.xinkk).setVisibility(INVISIBLE);//双击出心动画完成后 空心隐藏
                        findViewById(R.id.xin2).setVisibility(VISIBLE);//红心显示
                        BarrageView barrage = findViewById(R.id.braay);
                        barrage.addBarrage(new Barrage("喜欢了此视频"));
                        Video video = new Video();
                        video.getTitle();
                        video.getVideopath();
                        VideoMsgUtils.getInstance().getDaoSession().insert(video);
                        int size = VideoMsgUtils.getInstance().getDaoSession().queryBuilder().list().size();
                        ToastUtils.showShort("收藏成功!已收藏"+size);

                    }else{
                        time=System.currentTimeMillis();//不是双击时再次获取当前时间点
                    }
                }
                break;
        }
        return super.onTouch(v, event);
    }

    @Override//绑定视图
    public int getLayoutId() {
        return R.layout.mystand;
    }

    @Override//让双击暂停播放失效
    protected void touchDoubleUp(MotionEvent e) {
//        super.touchDoubleUp(e);
    }

}
